<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">

          <el-form-item label="用户名" prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户名"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="手机号码" prop="userPhone">
            <el-input
              v-model="queryParams.userPhone"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="订单号" prop="orderSn">
            <el-input
              v-model="queryParams.orderSn"
              placeholder="请输入订单号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="类型" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in statusList"
                :key="dict.label"
                :label="dict.value"
                :value="dict.label"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="公司" prop="companyId" v-if="companyId == null">
            <el-select
              v-model="queryParams.companyId"
              placeholder="公司"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in companyList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="是否逾期" prop="overStatus">
            <el-select
              v-model="queryParams.overStatus"
              placeholder="逾期状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in overStatusList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="借款类型" prop="applyType">
            <el-select
              v-model="queryParams.applyType"
              placeholder="借款类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in applyTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <!--<el-form-item label="分配时间">-->
            <!--<el-date-picker-->
              <!--v-model="dateRange"-->
              <!--style="width: 240px"-->
              <!--value-format="yyyy-MM-dd"-->
              <!--type="daterange"-->
              <!--range-separator="-"-->
              <!--start-placeholder="开始日期"-->
              <!--end-placeholder="结束日期"-->
            <!--&gt;</el-date-picker>-->
          <!--</el-form-item>-->

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!--<el-row :gutter="10" class="mb8">-->
          <!--<el-col :span="1.5">-->
            <!--<el-button-->
              <!--type="danger"-->
              <!--plain-->
              <!--icon="el-icon-delete"-->
              <!--size="mini"-->
              <!--:disabled="multiple"-->
              <!--@click="handleDelete"-->
            <!--&gt;批量关闭-->
            <!--</el-button>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="平台" align="center" prop="companyId"/>
          <el-table-column label="订单号" align="center" prop="orderSn"/>
          <el-table-column label="贷款用户" align="center" prop="userName"/>
          <el-table-column label="手机号码" align="center" prop="userPhone"/>
          <!--<el-table-column label="贷款金额" align="center" prop="applyMoney"/>-->
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <span v-if="scope.row.delFlag == '2'">
                订单关闭
              </span>
              <span v-else>
                <span v-for="item in statusList" v-if="scope.row.status == item.label">{{ item.value }}</span>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="申请时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="打款时间" align="center" prop="applyTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.applyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="还款时间" align="center" prop="repaymentTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.repaymentTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            fixed="right"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
              >
                <router-link :to="'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId" class="link-type">
                  <span>查看</span>
                </router-link>
              </el-button>
<!--              <el-button-->
<!--                size="mini"-->
<!--                type="text"-->
<!--                icon="el-icon-delete"-->
<!--                @click="handleDelete(scope.row)"-->
<!--              >审核-->
<!--              </el-button>-->
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >关闭
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {listApplyNew, examineApply, closeApply} from "@/api/app/apply";
import {list} from "@/api/company";

export default {
  name: "Apply",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      applyList: null,
      // 公司数据
      companyList: null,
      companyId: this.$store.state.user.companyId,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        companyId: undefined,
        status: undefined,
        overStatus: undefined,
        applyType: undefined,
        userPhone:undefined,
        orderSn: undefined
      },
      statusList: [
        {
          label: '999',
          value: '机审中'
        },
        {
          label: '0',
          value: '待初审'
        },
        {
          label: '1',
          value: '初审拒绝'
        },
        {
          label: '2',
          value: '待复审'
        },
        {
          label: '3',
          value: '审核拒绝'
        },
        {
          label: '4',
          value: '待提现'
        },
        {
          label: '5',
          value: '提现中'
        },
        {
          label: '6',
          value: '待还款'
        },
        {
          label: '7',
          value: '已还款'
        },
        {
          label: '8',
          value: '已逾期'
        },
        {
          label: '9',
          value: '放款中'
        },
        {
          label: '10',
          value: '放款失败'
        },
        {
          label: '11',
          value: '提现取消'
        },
      ],
      overStatusList: [
        {
          value: '0',
          label: '否'
        },
        {
          value: '1',
          label: '是'
        },
      ],
      applyTypeList: [
        {
          value: 0,
          label:'首次首借'
        },
        {
          value: 1,
          label:'复借1'
        },
        {
          value: 2,
          label:'复借2'
        },
        {
          value: 3,
          label:'复借3'
        },  {
          value: 4,
          label:'复借4'
        },
        {
          value: 5,
          label:'复借5'
        },
        {
          value: 6,
          label:'复借6'
        },
        {
          value: 7,
          label:'复借7'
        },

      ],

      // 表单校验
      rules: {
        applyMoney: [
          {required: true, message: "贷款不能为空", trigger: "blur"},
        ]
      }
    };
  },
  watch: {},
  created() {
    this.getList();
    if (!this.companyId) {
      this.getListCompany();
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // if (this.companyId) {
      //   this.queryParams.companyId = this.companyId
      // }
      listApplyNew(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getListCompany() {
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.companyList = response.data;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        applyMoney: 0,
        manageMoney: 0,
        examineMoney: 0,
        serviceMoney: 0,
        cycle: 0,
        realMoney: 0,
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getApply(id).then(response => {
        this.form = response.data;
        this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
        this.open = true;
        this.title = "修改";
      });
    },


    /** 提交按钮 */
    submitForm: function () {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != undefined) {
      //       updateIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认关闭编号为"' + ids + '"的数据项？').then(function () {
        return closeApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    // handleMoney(){
    //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    // }
  }
};
</script>
